const fs = require("fs");
const reStatic = /^\/files\/(.*)$/;

function readStatic(req, res) {
    // 同步读取（res死等data的读取结果）
    // const data = fs.readFileSync("./public/pages/login.html")
    // res.end(data)

    // 异步读取
    console.log("readStatic req.url",req.url);
    fs.readFile(`./public/${reStatic.exec(req.url)[1]}`, (err, data) => {
        if (err) {
            res.end(JSON.stringify(err));
        } else {
            res.end(data);
        }
    });
}

module.exports = {
    readStatic,
};
